const db = wx.cloud.database()
const works = db.collection('works')
import { formatDay } from "../../utils/myMoment";

Page({

  /**
   * 处理完成按钮
   */
  finish() {
    const { work } = this.data
    // 反转当前状态
    const status = !work.status
    works.doc(work._id).update({
      data: { status }
    }).then(() => {
      this.setData({ 'work.status': status })
    })
  },

  /**
   * 保存他人分享的 Work
   */
  saveShare() {
    const { title, course, context, end } = this.data.work
    const work = {
      title, course, context, end,
      status: false,
      create_time: new Date().valueOf()
    }
    works.add({ data: work }).then(() => {
      wx.showToast({ title: '添加成功', })
      setTimeout(() => {
        wx.reLaunch({ url: '/pages/index/index', })
      }, 1000)
    }).catch(e => {
      wx.showToast({ title: '保存失败！', icon: 'none' })
    })
  },

  /**
   * 显示/关闭 更多菜单
   */
  moreShow() {
    this.setData({ moreShow: !this.data.moreShow })
  },

  /**
   * 分配 更多菜单 事件
   */
  more(event) {
    switch (event.detail.name) {
      case '修改':
        this.setData({ updateShow: true })
        break
      case '删除':
        this.delete()
        break
    }
  },

  /**
   * 关闭 更新表单
   */
  updateClose() {
    this.setData({ updateShow: false })
  },

  /**
   * 提交 更新
   */
  async updateSubmit(event) {
    const newWork = event.detail.form
    console.log(newWork);
    const work = { ...this.data.work, ...newWork }
    await works.doc(work._id).update({ data: newWork })
    wx.showToast({ title: '更新成功', })
    this.setData({ work, updateShow: false })
  },

  /**
   * 处理 删除
   */
  async delete() {
    wx.showModal({
      title: '删除',
      confirmColor: '#ee0a24',
      content: '确定删除此条作业？',
      success: res => {
        if (res.confirm) {
          works.doc(this.data.work._id).remove().then(() => {
            wx.showToast({
              title: '删除成功',
              success: () => {
                setTimeout(() => {
                  wx.navigateBack()
                }, 1000)
              }
            })
          }
          )
        }
      }
    })
  },

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    updateShow: false,
    moreShow: false,
    moreActions: [
      { name: '修改' },
      { name: '删除', color: '#ee0a24' },
      { name: '分享给他人', color: '#07c160', openType: 'share' },
    ],
    work: {
      endDay: '',
      title: '',
      course: '',
      context: '',
      status: ''
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const work = await works.doc(options.id).get()
      .then(res => { return res.data })
    const _openid = await wx.cloud
      .callFunction({ name: 'getOpenId' })
      .then(res => { return res.result.openId })
    // 以后允许无结束日期
    if (!!work.end) {
      work.endDay = formatDay(work.end)
    }
    this.setData({
      work,
      loading: false,
      // 如果 work 的 openid 不是当前用户的 id , 则是分享
      share: work._openid !== _openid
    })
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    this.setData({ moreShow: false })
    return {
      title: this.data.work.title,
      path: '/pages/info/info?id=' + this.data.work._id
    }
  }
})